// adminChangePass.js
const db = wx.cloud.database();

Page({
  data: {
    oldUser: '',
    oldPass: '',
    newUser: '',
    newPass: ''
  },

  handleOldUserInput(e) {
    const { value } = e.detail;
    this.setData({
      oldUser: value
    });
  },

  handleOldPassInput(e) {
    const { value } = e.detail;
    this.setData({
      oldPass: value
    });
  },

  handleNewUserInput(e) {
    const { value } = e.detail;
    this.setData({
      newUser: value
    });
  },

  handleNewPassInput(e) {
    const { value } = e.detail;
    this.setData({
      newPass: value
    });
  },

  async changeAccount() {
    const { oldUser, oldPass, newUser, newPass } = this.data;
    
    try {
      // 查询原用户信息
      const queryResult = await db.collection('zyzl_admin').where({
        username: oldUser,
        password: oldPass
      }).get();

      // 如果查询到原用户信息，则执行修改操作
      if (queryResult.data.length > 0) {
        await db.collection('zyzl_admin').where({
          username: oldUser
        }).update({
          data: {
            username: newUser,
            password: newPass
          }
        });
        
        wx.showToast({
          title: '账号修改成功！',
          icon: 'success'
        });
      } else {
        wx.showToast({
          title: '原用户名或密码错误！',
          icon: 'none'
        });
      }
    } catch (error) {
      console.error('修改账号失败：', error);
      wx.showToast({
        title: '修改账号失败，请稍后重试！',
        icon: 'none'
      });
    }
  }
});
